<?php
// +----------------------------------------------------------------------
// | Bwsaas
// +----------------------------------------------------------------------
// | Copyright (c) 2015~2020 http://www.buwangyun.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Gitee ( https://gitee.com/buwangyun/bwsaas )
// +----------------------------------------------------------------------
// | Author: buwangyun <hnlg666@163.com>
// +----------------------------------------------------------------------
// | Date: 2020-9-28 10:55:00
// +----------------------------------------------------------------------

namespace buwang\subscribe;

use app\manage\model\AuthNode;
use app\common\model\SysLog;
use buwang\jobs\SysLogAutoCencel;
use buwang\base\Queue;

/**
 * 系统事件
 * Class ProductSubscribe
 * @package buwang\subscribes
 */
class SystemSubscribe
{
    public function handle()
    {
    }

    /**
     * 请求到达事件
     * @param array $event
     */
    public function onRequestStart($event)
    {
        list($request, $scopes, $node_name, $user, $code, $err_msg, $err_code, $force) = $event;

        //如果是租户或者总管理员则记录下请求信息
        $data = [];
        $is_add = false;
        if ($scopes == 'member') {
            $is_add = true;
            //记录用户信息
            if ($user) {
                $data['member_id'] = $user['id'];
                $data['name'] = $user['nickname'];
                $data['username'] = $user['username'];
            }
        }
        if ($scopes == 'admin') {
            $is_add = true;
            //记录用户信息
            if ($user) {
                $data['admin_id'] = $user['id'];
                $data['name'] = $user['nickname'];
                $data['username'] = $user['username'];
            }
        }
        $param = $request->param();
        //不记录分页的请求
        if ($is_add && !isset($param['page'])) {
            $ip = get_real_ip();
            $url = $request->url();//路由url
            $method = AuthNode::where('menu_path', $url)->value('title');
            //如果是登录则记录下地址
            if ($node_name == '/manage/admin.Index/login' || $node_name == '/manage/member.Index/login') {
                $city = SysLog::getCityByIp($ip);
                if ($city) $data['city_name'] = $city['country'] . '  ' . $city['regionName'] . '  ' . $city['city'] . '  ' . $city['zip'];
            }
            if (!$method) $method = '';
//            if($force===true){
//                if($err_code)$data['err_msg'] = $err_msg;
//                $data['code'] = $code;
//                $data['err_code'] = $err_code;
//            }
            $data['method'] = $method;
            $data['menu_path'] = $url;
            $data['path'] = $node_name;
            $data['param'] = json_encode($param);
            $data['ip'] = $ip;
            $data['scopes'] = $scopes;
            $data['add_time'] = time();
            $sysLog = SysLog::create($data);
            //得到执行清除时间
            $sys_log_time = bw_config('sys_log_time', 604800);
            if ($sys_log_time <= 0) return true;
            //放入清除队列
            Queue::instance()->job(SysLogAutoCencel::class)->secs($sys_log_time)->data($sysLog)->push();
        }
    }

    /**
     * 响应结束事件
     * @param array $event
     */
    public function onResponseEnd($event)
    {
        list($response) = $event;
    }

    /**
     * 安装应用事件
     * @param array $event
     */
    public function onMiniappInstall($event)
    {
        list($miniapp) = $event;//app对象

        \buwang\service\MiniappService::installConfigAndGroupData($miniapp->dir);//检测安装配置
        //应用安装成功事件
        set_miniapp_database_prefix($miniapp->dir);//跨应用调用model前调用此方法设置应用表前缀
        \buwang\service\MiniappService::handle($miniapp->dir, $event, 'miniappInstall');
    }

    /**
     * 购买应用事件
     * @param array $event
     */
    public function onMiniappBuySuccess($event)
    {
        list($member_id, $miniapp, $module) = $event; //app对象

        //购买应用成功事件
        set_miniapp_database_prefix($miniapp->dir);//跨应用调用model前调用此方法设置应用表前缀
        \buwang\service\MiniappService::handle($miniapp->dir, $event, 'miniappBuySuccess');
    }
}